package com.sy.pet.commom;

import cn.hutool.core.codec.Base64Decoder;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

/**
 * @Author: liangSY
 * @Date: 2021/6/25 9:41
 * @ClassName: Fileutils
 */
@Slf4j
public class Fileutils {

    /**
     * 写入文件
     *
     * @param target
     * @throws IOException
     */
    public static void write(String target, File file) throws IOException {
        InputStream src = new FileInputStream(file);
        OutputStream os = new FileOutputStream(target);
        byte[] buf = new byte[1024];
        int len;
        while (-1 != (len = src.read(buf))) {
            os.write(buf, 0, len);
        }
        os.flush();
        os.close();
    }

    public static void writeByBasr64(String img, String savePath) throws IOException {
        Base64Decoder de = new Base64Decoder();
        String imageFile = img.substring(img.indexOf(",") + 1);
        byte[] b = de.decode(imageFile);
        for (int i = 0; i < b.length; ++i) {
            if (b[i] < 0) {// 调整异常数据
                b[i] += 256;
            }
        }
        OutputStream out = new FileOutputStream(savePath);
        out.write(b);
        out.flush();
        out.close();
    }


    /**
     * 获取文件扩展名
     *
     * @param fileName
     * @return
     */
    public static String getExt(String fileName) {
        return fileName.substring(fileName.lastIndexOf(".") + 1);
    }

    /**
     * 删除文件
     *
     * @param filePath
     * @return
     */
    public static void delete(String filePath) {
        File file = new File(filePath);
        if (file.exists()) {
            file.delete();
        } else {
            log.info("文件删除失败{},找不到指定路径：" + filePath);
        }
    }

    public static String uploadFile(String fileRootPath, MultipartFile file, String folder) {
        if (file.isEmpty()) {
            throw new RuntimeException("文件上传失败");
        }
        String fileName = file.getOriginalFilename();
        String ext = Fileutils.getExt(fileName);
        String path = "/";
        if (StringUtils.isNotBlank(folder)) {
            path = path + folder + "/";
        }
        String rpath = fileRootPath + File.separator + path;
        File fileDir = new File(rpath);
        if (!fileDir.exists()) {
            fileDir.mkdirs();
        }
        String nFileName = IdUtil.randomUUID() + "." + ext;
        try {
            log.info("文件上传 {}" +  rpath + nFileName );
//            write(rpath + nFileName, file);
            file.transferTo(new File(rpath + nFileName));
        } catch (IOException e) {
            log.error("文件上传失败 {} " + e.getMessage(), e);
            throw new RuntimeException("文件上传失败");
        } catch (Exception e) {
            log.error("文件上传失败 {} " + e.getMessage(), e);
            throw new RuntimeException("文件上传失败");
        }
        return path+nFileName;
    }
}
